{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from tensorflow.examples.tutorials.mnist import input_data\n",
    "import tensorflow as tf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting mnist_data/train-images-idx3-ubyte.gz\n",
      "Extracting mnist_data/train-labels-idx1-ubyte.gz\n",
      "Extracting mnist_data/t10k-images-idx3-ubyte.gz\n",
      "Extracting mnist_data/t10k-labels-idx1-ubyte.gz\n"
     ]
    }
   ],
   "source": [
    "class mnist_cnn:\n",
    "    mnist = input_data.read_data_sets('mnist_data/',one_hot = True)\n",
    "\n",
    "    sess = tf.InteractiveSession()\n",
    "    \n",
    "    def weights_variables(self,shape):\n",
    "        initial = tf.truncated_normal(shape, stddev = 0.1)\n",
    "        return tf.Variable(initial)\n",
    "\n",
    "    def bias_variables(self,shape):\n",
    "        initial = tf.constant( 0.1, shape = shape)\n",
    "        return tf.Variable(initial)\n",
    "\n",
    "    def conv2d(self, x, W):\n",
    "        return tf.nn.conv2d( x, W, strides = [1,1,1,1], padding = 'SAME')\n",
    "\n",
    "    def max_pool_2x2(self,x):\n",
    "        return tf.nn.max_pool( x, ksize = [1,2,2,1], strides = [1,2,2,1], padding = 'SAME')\n",
    "    def train(self):\n",
    "        x = tf.placeholder( tf.float32, [None,784])\n",
    "        y_ = tf.placeholder( tf.float32, [None, 10])\n",
    "        x_images = tf.reshape(x , [-1,28,28,1])\n",
    "\n",
    "        W_conv1 = weights_variables([5,5,1,32])\n",
    "        b_conv1 = bias_variables([32])\n",
    "        h_conv1 = tf.nn.relu(conv2d(x_images , W_conv1) + b_conv1)\n",
    "        h_pool1 = max_pool_2x2(h_conv1)\n",
    "\n",
    "        W_conv2 = weights_variables([5,5,32,64])\n",
    "        b_conv2 = bias_variables([64])\n",
    "        h_conv2 = tf.nn.relu(conv2d(h_pool1 , W_conv2) + b_conv2)\n",
    "        h_pool2 = max_pool_2x2(h_conv2)\n",
    "\n",
    "        W_fc1 = weights_variables([7*7*64,1024])\n",
    "        b_fc1 = bias_variables([1024])\n",
    "        h_pool2_flat = tf.reshape(h_pool2 , [-1,7*7*64])\n",
    "        h_fc1 = tf.nn.relu( tf.matmul(h_pool2_flat , W_fc1) + b_fc1)\n",
    "\n",
    "        keep_prob = tf.placeholder(tf.float32)\n",
    "        h_fc1_drop = tf.nn.dropout( h_fc1 , keep_prob)\n",
    "\n",
    "        W_fc2 = weights_variables([1024,10])\n",
    "        b_fc2 =bias_variables([10])\n",
    "        y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop , W_fc2) + b_fc2)\n",
    "\n",
    "        cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y_conv), reduction_indices= [1]))\n",
    "        train_step = tf.train.AdamOptimizer(0.0001).minimize(cross_entropy)\n",
    "\n",
    "        correct_prediction = tf.equal(tf.argmax(y_conv,1),tf.argmax(y_,1))\n",
    "        accuracy = tf.reduce_mean(tf.cast( correct_prediction , tf.float32))\n",
    "\n",
    "        tf.global_variables_initializer().run()\n",
    "        for i in range(2000):\n",
    "            batch = mnist.train.next_batch(100)\n",
    "            if i%500 == 0 :\n",
    "                train_acurracy = self.sess.run(accuracy,feed_dict = { x : batch[0] , y_ : batch[1] , keep_prob : 1.0})\n",
    "                print( 'step %d ,training accuracy %.5f '%(i , train_acurracy))\n",
    "            train_step.run(feed_dict = { x : batch[0] , y_ : batch[1] , keep_prob : 0.5})\n",
    "\n",
    "        print('test accuracy %.5f '%accuracy.eval(feed_dict = { x : mnist.test.images , y_ : mnist.test.labels , keep_prob : 1.0}))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "a = mnist_cnn()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "step 0 ,training accuracy 0.07000 \n",
      "step 500 ,training accuracy 0.96000 \n",
      "step 1000 ,training accuracy 0.99000 \n",
      "step 1500 ,training accuracy 0.97000 \n",
      "test accuracy 0.98060 \n"
     ]
    }
   ],
   "source": [
    "a.train()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "Non-ASCII character '\\xe5' in file mnist_reconition.py on line 12, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details (mnist_reconition.py, line 12)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  File \u001b[0;32m\"mnist_reconition.py\"\u001b[0;36m, line \u001b[0;32m12\u001b[0m\n\u001b[0;31m    def rgb2gray(rgb):#分成若干个小格子得到max_pooling的值\u001b[0m\n\u001b[0m                                              ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m Non-ASCII character '\\xe5' in file mnist_reconition.py on line 12, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details\n"
     ]
    }
   ],
   "source": [
    "import mnist_reconition as mr\n",
    "\n",
    "s_features, s_labels = mr.re_showdigits(1)\n",
    "\n",
    "random_test_predictions = sess.run(tf.nn.top_k(y_conv, 10),feed_dict={x: s_features, y_: s_labels , keep_prob : 1.0})\n",
    "print(random_test_predictions)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:tensorflow]",
   "language": "python",
   "name": "conda-env-tensorflow-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
